{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Adding a Time Trigger to a Lattice\n",
    "\n",
    "This example illustrates how to use a covalent.trigger.TimeTrigger to trigger workflow dispatches automatically at a specified interval.\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "Import Covalent and the trigger."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "from covalent.triggers import TimeTrigger"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Procedure\n",
    "\n",
    "1. Create a `TimeTrigger` object that performs a trigger action every 5 seconds."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "time_trigger = TimeTrigger(time_gap=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Create a workflow:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "@ct.lattice\n",
    "@ct.electron\n",
    "def my_workflow():\n",
    "    return 42"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Dispatch `my_workflow`, disabling its first execution using the `disable_run` parameter in `ct.dispatch`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "44f56fcf-96dc-4089-84d4-069fd13e3e58\n"
     ]
    }
   ],
   "source": [
    "dispatch_id = ct.dispatch(my_workflow)()\n",
    "print(dispatch_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. Attach the trigger to the `dispatch_id` and register it with the trigger server."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "time_trigger.lattice_dispatch_id = dispatch_id\n",
    "time_trigger.register()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. Monitor the Covalent UI. Watch the Dashboard for new dispatches of `my_workflow`.\n",
    "\n",
    "6. In the Covalent UI, observe that a new `my_workflow` is dispatched every five seconds.\n",
    "\n",
    "7. To disable triggers on the dispatch, use the `ct.stop_triggers` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2023-09-25 08:51:25,893] [DEBUG] local.py: Line 334 in stop_triggers: Triggers for following dispatch_ids have stopped observing:\n",
      "[2023-09-25 08:51:25,894] [DEBUG] local.py: Line 336 in stop_triggers: 44f56fcf-96dc-4089-84d4-069fd13e3e58\n"
     ]
    }
   ],
   "source": [
    "ct.stop_triggers(dispatch_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the `stop_triggers` function disables all triggers attached to the specified dispatch. \n",
    "\n",
    "## See Also\n",
    "\n",
    "[Adding a Directory Trigger to a Lattice](./trigger_dir.ipynb)\n",
    "\n",
    "[Adding a SQLite Trigger to a Lattice](./trigger_sqlite.ipynb)\n",
    "\n",
    "[Adding a Database Trigger to a Lattice](./trigger_database.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  },
  "vscode": {
   "interpreter": {
    "hash": "ffe78875ce1aa6161f50f6a6dec2555e7255bbdb44cc39b93c0dfc1daa8da522"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
